<?php
/*******************************************************************************
 * @filename: transactions_controller.php
 *
 * @author: immer_baldos
 *
 * @description: controller for transaction model
 *
 * @history:
 *
 *   VERSION   DATE                   BY          NOTES
 *  ---------------------------------------------------------------
 *     1.0     September  9, 2009     immer       initial bake
 *
 *     2.0     September 12, 2009     immer       enable auth and add oneentry()
 *
 *     3.0     September 17, 2009     immer       removed admin routes
 *
 ******************************************************************************/
class TransactionsController extends AppController {

	var $name = 'Transactions';
	var $helpers = array('Html', 'Form');
    var $components = array('Auth','Cookie', 'Session');

	function index() {
		$this->Transaction->recursive = 0;
		$this->set('transactions', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Transaction.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('transaction', $this->Transaction->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Transaction->create();
			if ($this->Transaction->save($this->data)) {
				$this->Session->setFlash(__('The Transaction has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Transaction could not be saved. Please, try again.', true));
			}
		}
		$orders = $this->Transaction->Order->find('list');
		$customers = $this->Transaction->Customer->find('list');
		$products = $this->Transaction->Product->find('list');
		$this->set(compact('orders', 'customers', 'products'));
	}

    //add one transaction entry using order,customer and product id, with default count of 1
	function oneentry($OrderId = null, $CustomerId = null, $ProductId = null, $ProductCnt = 1) {
	    //requires customer has an order
	    $orderCheck = $this->Transaction->Order->find('first',
	        array('conditions' => array('customer_id' => $CustomerId))
	    );

	    //requires customer exists
	    $customerCheck = $this->Transaction->Customer->read(null, $CustomerId);

	    //requires product exists
	    $productCheck = $this->Transaction->Product->read(null, $ProductId);

        if (!empty($orderCheck) && !empty($customerCheck) && !empty($productCheck)) {
    		$products = $this->Transaction->Product->read(null,$ProductId);

            //to many orders filter
            if (($products['Product']['instock']-$ProductCnt) < 0 ){
    			$this->Session->setFlash(__('There is not enough stock for that product.', true));
    			$this->redirect($this->referer());
            }

            //reduce available stock
            $products['Product']['instock']-=$ProductCnt;
            $this->Transaction->Product->save($products);

    	    $mydata=array('price'       =>  $products['Product']['price'],
    	                  'order_id'    =>  $OrderId,
    	                  'count'       =>  $ProductCnt,
    	                  'customer_id' =>  $CustomerId,
    	                  'product_id'  =>  $ProductId
    	                  );
    	    $this->Transaction->create();
    		if ($this->Transaction->save($mydata)) {
    			$this->redirect($this->referer());
    		}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Transaction', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Transaction->save($this->data)) {
				$this->Session->setFlash(__('The Transaction has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Transaction could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Transaction->read(null, $id);
		}
		$orders = $this->Transaction->Order->find('list');
		$customers = $this->Transaction->Customer->find('list');
		$products = $this->Transaction->Product->find('list');
		$this->set(compact('orders','customers','products'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Item', true));
    			$this->redirect($this->referer());
		}

		//uncommit stocks
		$transaction = $this->Transaction->read(null,$id);

	    $this->loadModel('Product');
        $product = $this->Product->read( null ,$transaction['Transaction']['product_id']);
        $product['Product']['instock']+=$transaction['Transaction']['count'];
        $this->Product->save($product);

		if ($this->Transaction->del($id)) {
			$this->Session->setFlash(__('Item deleted', true));
    			$this->redirect($this->referer());
		}
	}


}
?>